import 'package:flutter/material.dart';
import 'NestedLayer.dart';

class NestedLayerInputPage extends StatefulWidget {
  const NestedLayerInputPage({super.key});

  @override
  State<NestedLayerInputPage> createState() => _NestedLayerInputPageState();
}

class _NestedLayerInputPageState extends State<NestedLayerInputPage> {
  final TextEditingController _controller = TextEditingController();
  String? _errorText;

  void _onGeneratePressed() {
    final input = _controller.text;
    final depth = int.tryParse(input);

    if (depth == null || depth < 0 || depth > 5000) {
      setState(() {
        _errorText = "input layer number";
      });
      return;
    }

    Navigator.of(context).push(
      MaterialPageRoute(
        builder: (_) => NestedLayer(depth: depth),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("输入嵌套层数")),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: _controller,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                labelText: "请输入嵌套层数",
                errorText: _errorText,
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _onGeneratePressed,
              child: const Text("生成 layers"),
            ),
          ],
        ),
      ),
    );
  }
}
